package com.anXin.article.controller;


import com.anXin.apis.user.IUserClient;
import com.anXin.article.service.ReplyChildService;
import com.anXin.article.service.ReplyService;
import com.anXin.model.article.dtos.ReplyDto;
import com.anXin.model.article.pojos.Reply;
import com.anXin.model.article.pojos.ReplyChild;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import utils.common.DateUtils;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/app/reply")
public class ReplyController {

    @Resource
    private ReplyService replyService;

    @Resource
    private ReplyChildService replyChildService;

    @Resource
    private IUserClient userClient;
    /**
     * 查询一级评论√
     * @param articleId
     * @return
     */
    @GetMapping("/{articleId}")
    public ResponseResult findReply(@PathVariable("articleId") Long articleId){
        List<Reply> lists= replyService.query().eq("article_id",articleId).list();
        List<Reply> replyList = lists.stream().map(reply -> {
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(reply.getUserId()).getData();
            reply.setAvatar(data.get("image").toString());
            reply.setUsername(data.get("name").toString());
            return reply;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(replyList);
        }


    /**
     * 发布一级评论√
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public  ResponseResult addReply(@RequestBody ReplyDto dto){
        Reply reply = new Reply();
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        reply.setUsername(data.get("name").toString());
        reply.setAvatar(data.get("image").toString());
        reply.setContent(dto.getContent());
        reply.setArticleId(Long.valueOf(dto.getArticleId()));
        reply.setUserId(AppThreadLocalUtil.getUser().getId());
        replyService.save(reply);
        return ResponseResult.okResult("发布成功");
    }

   /*删除评论,参数文章id*/
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") Long id){
        return replyService.deleteReply(id);
    }






}
